<?php
require("../data/DataHandler.php");
session_start();
$data_handler = DataHandler::singleton();
//Log out
if (isset($_POST['logout'])) {
  unset($_SESSION['loggedIn']);
}
//If the person has already accessed the site
if (isset($_SESSION['loggedIn'])) {
  //Authentication
  if (isset($_POST['name']) && isset($_POST['password'])) {
    $name = $_POST["name"];
    $password = $_POST["password"];
    if (!($name == "") && $data_handler->authenticate($name, $password)) {
      $_SESSION['loggedIn'] = true;
    } else
      $_SESSION['loggedIn'] = false;
  }
  //Only perform these actions if user has been authenticated
  if ($_SESSION['loggedIn']) {
    
  }
} else
  $_SESSION['loggedIn'] = false;
//View app table
?>
<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <title>GARS</title>
    <link rel = "stylesheet" type= "text/css" href = "../styles/basic.css" />
    <script src = "../scripts/jquery.js"></script>
    <script src = "../scripts/jquery-validation/jquery.validate.js"></script>
    <script>
      //JQuery
      $(document).ready(function(){
        //form validation
        $("#loginForm").validate();
        $("#uploadForm").validate();
        //table tabbing
        $('#otsTable').toggle(false);
        $('#viewAY').click(function(){
          $('#ayTable').toggle(true);
          $('#otsTable').toggle(false);
        });
        $('#viewOTS').click(function(){
          $('#ayTable').toggle(false);
          $('#otsTable').toggle(true);
        });
      });
    </script>
  </head>
  <body>
    <div class = "wrapper">
      <?php
      //Load logged in page
      if (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']) {
        $name = $_SESSION['name'];
        $role = $_SESSION['role'];
        echo "<p>Welcome back, $name!</p>";
        //load role functions
        if ($role == "CHAIR") {
          //chair functions
          require("../includes/chair.html");
        } else if ($role == "STAFF") {
          //staff functions
          require("../includes/staff.html");
        } else if ($role == "FACULTY") {
          //faculty functions
          require("../includes/staff.html");
        }
        //View application table
        if (isset($_POST['viewAppTable'])) {
          echo '<input type = "button" value = "View AY" id = "viewAY" />
                <input type = "button" value = "View OTS" id = "viewOTS" />
               ';
          echo '<div id = "ayTable">';
          $data_handler->printTable("aydata", TRUE, "border='2'");
          echo '</div>
                <div id = "otsTable">';
          $data_handler->printTable("otsdata");
          echo '</div>';
        }
        //Load authentication page
      } else {
        require("../includes/login.html");
      }
      ?>
    </div>
  </body>
</html>